package csse3004.GD.PAdmin;

/**
 * @author Mamdouh Alharbi- s4194298
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import csse3004.GD.DA.PollAdminDA;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.web.servlet.mvc.Controller;

public class AssignPollCreatorController implements Controller {

    private PollAdminService pollAdminService = new PollAdminDA();
    protected final Log logger = LogFactory.getLog(getClass());
         
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) {
              
         if (hsr.getParameter("cid") != null && hsr.getParameter("pid") != null){     
            int cid = Integer.parseInt(hsr.getParameter("cid"));
            int pid = Integer.parseInt(hsr.getParameter("pid"));
            pollAdminService.assignToPollCreator(pid, cid);
            logger.info("in home controller ");
            logger.info("cid "+cid);
            logger.info("pid "+pid);
            String assigned="Poll creator has been assigned";           
            return new ModelAndView("assignPollCreator","assigned",assigned);
        }
       else if (hsr.getParameter("id") != null){
            ArrayList<AssignPollCreatorData> pollCreatorDetailList = new ArrayList<AssignPollCreatorData>();
            int pid = Integer.parseInt(hsr.getParameter("id"));
            logger.info("poll ID: "+pid);
            pollCreatorDetailList = pollAdminService.getUnassignedCreators(pid);
            return new ModelAndView("assignPollCreator","pollCreatorDetail", pollCreatorDetailList);
        }
        logger.info("not");
        return new ModelAndView("assignPollCreator");
    }
}
